package io.terminus.manager;

import io.terminus.chain.*;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * Created by Stay on 2017/6/24  17:13.
 */
public class FlagManager {

    private HashMap<String, Boolean> flagMap = new HashMap<>();

    private Integer[] spNums;

    private Integer judgeNum;

    private enum Singleton {
        INSTANCE;
        private FlagManager singleton;

        Singleton() {
            singleton = new FlagManager();
        }

        public FlagManager getInstance() {
            return singleton;
        }
    }

    public static FlagManager getInstance() {
        return Singleton.INSTANCE.getInstance();
    }

    /**
     * 接收特殊数字数组
     *
     * @param spNums
     * @return
     */
    public FlagManager reveiveArray(Integer[] spNums) {
        this.spNums = spNums;
        return this;
    }

    /**
     * 接收待处理的数字
     *
     * @param num
     * @return
     */
    public FlagManager reveiveNum(Integer num) {
        this.judgeNum = num;
        return this;
    }


    /**
     * 判断接收数字
     *
     * @return
     */
    public FlagManager judgeNum() {
        flagMap.put("oneFlag", this.judgeNum % spNums[0] == 0);
        flagMap.put("twoFlag", this.judgeNum % spNums[1] == 0);
        flagMap.put("threeFlag", this.judgeNum % spNums[2] == 0);
        flagMap.put("spFlag", StringUtils.countMatches(judgeNum.toString(), spNums[0].toString()) > 0 ? true : false);
        flagMap.put("allFlag", this.judgeNum % spNums[0] == 0 || this.judgeNum % spNums[1] == 0 || this.judgeNum % spNums[2] == 0);
        return this;
    }

    /**
     * 返回字符串
     *
     * @return
     */
    public String build() {
        Handler handler = new FirstHandler();
        SecondHandler secondHandler = new SecondHandler();
        ThirdHandler thirdHandler = new ThirdHandler();
        FourthHandler fourthHandler = new FourthHandler();
        handler.setNextHandler(secondHandler);
        secondHandler.setNextHandler(thirdHandler);
        thirdHandler.setNextHandler(fourthHandler);
        handler.handle(this.judgeNum);
        return fourthHandler.getStr();
    }


    private FlagManager() {
    }

    public HashMap<String, Boolean> getFlagMap() {
        return flagMap;
    }
}
